<?php
Yii::import('application.components.Reference');
Yii::import('application.components.U');
class KasController extends GxController
{
    public function actionCreate()
    {
        if (!Yii::app()->request->isAjaxRequest)
            $this->redirect(url('/'));
        if (isset($_POST) && !empty($_POST)) {
            $is_in = $_POST['arus'] == 1;
            $msg = "Data gagal disimpan.";
            $detils = CJSON::decode($_POST['detil']);
            app()->db->autoCommit = false;
            $transaction = Yii::app()->db->beginTransaction();
            try {
                $model = new Kas;
                $ref = new Reference();
                $docref = $ref->get_next_reference($is_in ? CASHIN : CASHOUT);
                foreach ($_POST as $k => $v) {
                    if ($k == 'detil' || $k == 'arus') continue;
                    if (is_angka($v)) $v = get_number($v);
                    $_POST['Kas'][$k] = $v;
                }
                $_POST['Kas']['doc_ref'] = $docref;
                $_POST['Kas']['tgl'] = new CDbExpression('NOW()');
                $_POST['Kas']['total'] = $is_in ? $_POST['Kas']['total'] : -$_POST['Kas']['total'];
                $model->attributes = $_POST['Kas'];
                if (!$model->save())
                    throw new Exception(t('save.model.fail','app',array('{model}'=>'Cash')). CHtml::errorSummary($model));
                foreach ($detils as $detil) {
                    $kas_detail = new KasDetail;
                    $_POST['KasDetail']['item_name'] = $detil['item_name'];
                    $_POST['KasDetail']['qty'] = get_number($detil['qty']);
                    $_POST['KasDetail']['price'] = $is_in ? get_number($detil['price']) : -get_number($detil['price']);
                    $_POST['KasDetail']['total'] = $is_in ? get_number($detil['total']) : -get_number($detil['total']);
                    $_POST['KasDetail']['kas_id'] = $model->kas_id;
                    $kas_detail->attributes = $_POST['KasDetail'];
                    if (!$kas_detail->save())
                        throw new Exception(t('save.model.fail','app',array('{model}'=>'Detail Cash')) . CHtml::errorSummary($kas_detail));
                }
                $ref->save($is_in ? CASHIN : CASHOUT, $model->kas_id, $docref);
                $msg = t('save.success','app');
                $transaction->commit();
                $status = true;
            } catch (Exception $ex) {
                $transaction->rollback();
                $status = false;
                $msg = $ex->getMessage();
            }
            echo CJSON::encode(array(
                'success' => $status,
                'id' => $docref,
                'msg' => $msg));
            Yii::app()->end();
        }
    }
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id, 'Kas');
        if (isset($_POST) && !empty($_POST)) {
            foreach ($_POST as $k => $v) {
                if (is_angka($v)) $v = get_number($v);
                $_POST['Kas'][$k] = $v;
            }
            $msg = "Data gagal disimpan";
            $model->attributes = $_POST['Kas'];
            if ($model->save()) {
                $status = true;
                $msg = "Data berhasil di simpan dengan id " . $model->kas_id;
            } else {
                $msg .= " " . CHtml::errorSummary($model);
                $status = false;
            }
            if (Yii::app()->request->isAjaxRequest) {
                echo CJSON::encode(array(
                    'success' => $status,
                    'msg' => $msg
                ));
                Yii::app()->end();
            } else {
                $this->redirect(array('view', 'id' => $model->kas_id));
            }
        }
    }
    public function actionIndex()
    {

        $criteria = new CDbCriteria();
        if (isset ($_POST['mode'])) {
            $criteria->select = "kas_id,doc_ref,no_kwitansi,keperluan,IF(total>=0,total,-total) total,bank_id,who,tgl,user_id,tdate";
            $criteria->addCondition($_POST['mode'] == 'masuk' ? "total >= 0" : "total < 0");
            $criteria->addCondition('DATE(tgl) = :tgl');
            $criteria->params = array(':tgl'=>$_POST['tgl']);
        }

        $model = Kas::model()->findAll($criteria);
        $total = Kas::model()->count($criteria);
        $this->renderJson($model, $total);
    }
}